﻿
namespace Data.Repository
{
	using System.Linq;
	using System.Collections.Generic;

	public class ProductTagRepository : Data.Repository.IProductTagRepository
	{
		private LinqToECommerceDBDataContext _dbContext;

		#region IProductTagRepository Members

		public int CreateNewProductTag(Data.Entity.ProductTag theNewProductTag)
		{
			if(theNewProductTag == null)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_ProductTag_Create(theNewProductTag.Name, theNewProductTag.LanguageID);
			}

			return result;
		}

		/// <summary>
		/// delete product tag method
		/// </summary>
		/// <param name="id"></param>
		/// <returns>0: failed 1: successed</returns>
		public int DeleteProductTag(int id)
		{
			if(id <= 0)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_ProductTag_Delete(id);
			}

			return result;
		}

		public IList<Data.Entity.ProductTag> GetProductTagByLanguage(int languageID)
		{
			List<ECommerce_GetProductTag_By_LanguageResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetProductTag_By_Language(languageID);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.ProductTag>();

				foreach(var t in lstData)
				{
					var theEntity = new Data.Entity.ProductTag()
					{
						Name = t.Name,
						ProductCount = t.ProductCount,
						ProductTagID = t.ProductTagID,
						LanguageID = t.LanguageID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public Data.Entity.ProductTag GetTheProductTag(int id)
		{
			if(id <= 0)
			{
				return null;
			}

			List<Data.ProductTag> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ProductTags.Where(exp => exp.ProductTagID == id);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var tag = lstData[0];

				Data.Entity.ProductTag result;

				result = new Data.Entity.ProductTag()
				{
					Name = tag.Name,
					ProductCount = tag.ProductCount,
					ProductTagID = tag.ProductTagID,
					LanguageID = tag.LanguageID
				};

				return result;
			}

			return null;
		}

		public int PlusOrMinus1ProductCount(int theProductTagID, int pn1)
		{
			if(theProductTagID <= 0)
			{
				return -1;
			}

			if(pn1 == 1 || pn1 == -1)
			{
				int result = 0;

				using(_dbContext = new LinqToECommerceDBDataContext())
				{
					var dbProductTag = (from tag in _dbContext.ProductTags
										where tag.ProductTagID.Equals(theProductTagID)
										select tag).FirstOrDefault();

					if(dbProductTag == null)
					{
						result = -1;
					}

					dbProductTag.ProductCount += pn1;

					_dbContext.SubmitChanges();

					result = 1;
				}

				return result;
			}
			else
			{
				return -1;
			}
		}

		#endregion
	}
}
